
<?php

class Pages extends Controller {

    function Pages() {
        parent::Controller();
	   	$this->load->library('email');
        $this->load->model(array('m_users', 'm_products'));
        $this->load->library(array("form_validation","pagination"));
    }
	//products on slide show for calling all functions by kimsong
	
    // function login...
    //=================================\\\\\\\\\\\\\
    function index() {
		
	  	$data['adver_img'] = $this->m_products->adver_img();
		$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
        $username = $this->input->post("username");
        $pass = md5($this->input->post("pass"));

        if ($username) {
            //die($username);
            $result['account'] = $this->m_users->getAlluser($username, $pass);

            if ($result['account']->num_rows() > 0) {
                $sess_array = array();
                foreach ($result['account']->result() as $row) {
                    $this->session->set_userdata('username', $row->user_username);
                    $this->session->set_userdata('pass', $row->user_password);
                    $this->session->set_userdata('id', $row->user_id);
					$this->session->set_userdata('statu', $row->user_statu_id);
                    $this->session->set_userdata('name', $row->user_fullName);
                    $this->session->set_userdata('admin', $row->user_role_id);

                    if ($row->user_role_id == 1) {
                        redirect('backend_pages/allproducts');
                    } else {
                        redirect('pages/user_profile');
                    }
                }
            } else {
                redirect("pages/news/error");
            }
        } else {
            $data['title'] = 'Welcome to Login page';
			$data['adver_img'] = $this->m_products->adver_img();
			$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
            $this->load->view('index', $data);
        }
    }
	// end login function ...............

	//search product result here....
	function search() {
        $search = $this->input->post('search_box');
        if (empty($search)) {
            $data['title'] = 'Result search';
            $data['search_error'] = "Please enter product name to search!";
		  	$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
			$data['adver_img'] = $this->m_products->adver_img();
            $this->load->view('index', $data);
        }else{
            $data['title'] = 'Result search';
            $data['query']=$this->m_products->sear_pro($search);
		  	$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
			$data['adver_img'] = $this->m_products->adver_img();
			
            $this->load->view('index', $data);
        }
    }
    //*******End search ********

    function advertise_page() {
        $this->load->view('pages/advertise_page');
    }

    // register user member in website
    function signup_account() {


        $this->form_validation->set_rules('gender', 'Gender', 'trim|required');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_alpha_numeric|min_length[6]|xss_clean|strtolower|callback_username_not_exists');
        $this->form_validation->set_rules('pass', 'Password', 'trim|required|alpha_alpha_numeric|min_length[6]|matches[con_pass]|xss_clean');
        $this->form_validation->set_rules('con_pass', 'Confirm_password', 'trim|required|alpha_alpha_numeric|min_length[6]');
        $this->form_validation->set_rules('phone', 'phone', 'trim|required|alpha_alpha_numeric|min_length[9]|max_lenght[10]');


        if ($this->form_validation->run() == FALSE) {
            //hasn't been run or there are validation errors
            $data['title'] = 'REGISTER ';
            // $data['menu'] = $this->menu->show_menu();
			$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
			
	  		$data['adver_img'] = $this->m_products->adver_img();
			
            $this->load->view('index', $data);
        } else {
            $result['reg'] = $this->m_users->register();

            if ($result) {
                redirect('pages/signup_account/success');
            } else {
                echo 'error';
            }
        }
    }

    // fun username not exists in record.....
    function username_not_exists($username) {
        $this->form_validation->set_message('username_not_exists', $this->input->post('username') . ' already exists.<br />
 Please choose a different from ' . $this->input->post('username') . '!');
        //redirect('con_login/login');
        if ($this->m_users->checkexist($username)) {
            return false;
        } else {
            return true;
        }
    }

    // ============ End check username exist ========***
	//pagination function for call===>


    //=====logout page kimsong and rina update together====
    function logout() {
        if ($this->session->userdata('id')) {
            $this->session->unset_userdata('username');
            $this->session->unset_userdata('id');
            //$this->session->sess_destroy();
            $this->session->sess_destroy();
            redirect('/');
        }
    }

    // end fun call pages =========\kisongsan/========
    //users login profile and all information about users.........
    function user_profile() {
		
		$config['base_url'] = site_url('pages/user_view_product');
        $config['total_rows'] = $this->db->count_all('tbl_product');
        
        $config['per_page'] = 6;
        $config["uri_segment"] = 3;
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $this->pagination->initialize($config);

        $data['pages'] = $this->pagination->create_links();

        //$data['all_advertising'] = $this->allproduct->all_advertising();
        $data['pro_list'] = $this->m_products->view_product($config['per_page'],$page);
		
		
        $data['account'] = $this->m_users->sel_account();
        $data['title'] = "User profile";
		$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
	  	$data['adver_img'] = $this->m_products->adver_img();
		
        $this->load->view('index', $data);
		
    }

    //update account of users by kimsong.san
    function user_update_account() {
        $data['account'] = $this->m_users->sel_account();

        $this->form_validation->set_rules('gender', 'Gender', 'trim|required');
        $this->form_validation->set_rules('username', 'Username', 'trim|required|alpha_alpha_numeric|min_length[6]|xss_clean|strtolower');
        $this->form_validation->set_rules('phone', 'phone', 'trim|required|alpha_alpha_numeric|min_length[9]|max_lenght[10]');

        if ($this->form_validation->run() == FALSE) {
            //hasn't been run or there are validation errors
            $data['title'] = 'Update account ';
			$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
	  		$data['adver_img'] = $this->m_products->adver_img();
			
            $this->load->view('index', $data);
        } else {
            $data['account'] = $this->m_users->update_account();
            if ($data['account'] > 0) {

                redirect('pages/user_profile/' . $this->session->userdata('id'));
            }
        }
    }

    //change password of users by kimsong.san
    function user_change_pass() {
		$this->form_validation->set_rules('pass', 'Password', 'trim|required|alpha_alpha_numeric|min_length[6]|matches[con_pass]|xss_clean');
        $this->form_validation->set_rules('con_pass', 'Confirm_password', 'trim|required|alpha_alpha_numeric|min_length[6]');
		if ($this->form_validation->run() == FALSE) {
			$data['title'] = 'Change Password';
			$data['sli_pro'] = $this->m_products->sl_pro();
			$data['adver_img'] = $this->m_products->adver_img();
			
			$this->load->view('index', $data);
		}else{
			//$data['account'] = $this->m_users->sel_account();
			$pass = $this->session->userdata('pass');
			$currentpass = $this->input->post("old_pass");
			if (isset($currentpass) && $currentpass != "") {
				$oldpass = md5($currentpass);
	
				if ($pass === $oldpass) {
					$data['pass'] = $this->m_users->udpate_pass();
	
					if ($data['pass'] > 0) {
						
						$this->logout();
					}
				} else {
					redirect('pages/user_change_pass/' . $this->session->userdata('id') . '/error');
				}
			}
		
		}
    }

    //add products of user login=====

 /*     * ***********************
     * upload images users login with resize images.........

     * ********************** */

    private $data = array(
        'dir' => array(
        'original' => 'assets/uploads/original/',
        'thumb' => 'assets/uploads/thumbs/'
        ),
        'total' => 0,
        'images' => array(),
        'error' => ''
    );

    public function user_add_products($start = 0) {
        if ($this->input->post('btn_upload')) {
            $this->upload();
        }

       // $this->load->library('pagination');  

        $c_paginate['base_url'] = site_url('pages/user_add_products');
        $c_paginate['per_page'] = '9';
        $finish = $start + $c_paginate['per_page'];

        if (is_dir($this->data['dir']['thumb'])){
            $i = 0;
            if ($dh = opendir($this->data['dir']['thumb'])) {
                while (($file = readdir($dh)) !== false) {
                    // get file extension
                    $ext = strrev(strstr(strrev($file), ".", TRUE));
                    if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png') {
                        if ($start <= $this->data['total'] && $this->data['total'] < $finish) {
                            $this->data['images'][$i]['thumb'] = $file;
                            $this->data['images'][$i]['original'] = str_replace('thumb_', '', $file);
                            $i++;
                        }
                        $this->data['total']++;
                    }
                }
                closedir($dh);
            }
        }


        $c_paginate['total_rows'] = $this->data['total'];
        $this->pagination->initialize($c_paginate);
        $data = $this->data;
		
        $data['title'] = 'Add products';
		$data['sli_pro'] = $this->m_products->sl_pro();
		$data['adver_img'] = $this->m_products->adver_img();
		
        $this->load->view('index', $data);
    }

    private function upload() {
        $c_upload['upload_path'] = $this->data['dir']['original'];
        $c_upload['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|JPG|PNG|x-png';
        $c_upload['max_size'] = '500';
        $c_upload['max_width'] = '1600';
        $c_upload['max_height'] = '1200';
        $c_upload['remove_spaces'] = TRUE;

        $this->load->library('upload', $c_upload);
		
        if ($this->upload->do_upload()) {

            $img = $this->upload->data();

            // create thumbnail
            $new_image = $this->data['dir']['thumb'] . 'thumb_' . $img['file_name'];

            $c_img_lib = array(
                'image_library' => 'gd2',
                'source_image' => $img['full_path'],
                'maintain_ratio' => TRUE,
                'width' => 100,
                'height' => 100,
                'new_image' => $new_image
            );

            $this->load->library('image_lib', $c_img_lib);
            $this->image_lib->resize();

            $img = $this->upload->data();
            //$this->session->set_userdata('Adver_Size',$img['image_size_str']);
            $this->session->set_userdata('pro_img', $img['file_name']);

          //  $this->load->model('M_addproduct');
           $data['product'] = $this->m_products->addproduct();
            if ($data['product'] > 0) {
                redirect('pages/user_view_product/addsucc');
            }
        } else {
            $this->data['error'] = $this->upload->display_errors();
        }
    }
	// fun delete db---
	public function delete($ori_img) {
        unlink($this->data['dir']['original'] . $ori_img);
        unlink($this->data['dir']['thumb'] . 'thumb_' . $ori_img);

        $id = $this->uri->segment(3);
        $oldrow = array('pro_img' => $id);
        $this->m_products->del_pro($oldrow);
		
        redirect('pages/user_view_product/dsucc');
    }
/*
* 	Select product to update....

	function user_sel_up_product(){
			$data['title'] = "Form update product";
			$data['upproduct'] = $this->m_products->sel_up();
			$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar.
			
			$this->load->view('index',$data);
		}
*/
	// udate data of product that users post...
	function user_sel_up_product(){
				if(!$this->input->post('btn_upload')){
				$data['title'] = "Form update product";
				$data['upproduct'] = $this->m_products->sel_up();
				$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar.
	  			$data['adver_img'] = $this->m_products->adver_img();
			
				$this->load->view('index',$data);
				}else{
				
					$data['up_prodata'] = $this->m_products->m_update_product();
					if ($data['up_prodata'] > 0) {
						
						redirect('pages/user_view_product');
					}
				}
			
		}
// update products of users.......

	 private $update = array(
        'dir' => array(
        'original' => 'assets/uploads/original/',
        'thumb' => 'assets/uploads/thumbs/'
        ),
        'total' => 0,
        'images' => array(),
        'error' => ''
    );

    public function change_product($start = 0) {
        if ($this->input->post('btn_upload')) {
            $this->upload_update();
        }

       // $this->load->library('pagination');  

        $c_paginate['base_url'] = site_url('pages/update_products');
        $c_paginate['per_page'] = '9';
        $finish = $start + $c_paginate['per_page'];

        if (is_dir($this->update['dir']['thumb'])){
            $i = 0;
            if ($dh = opendir($this->update['dir']['thumb'])) {
                while (($file = readdir($dh)) !== false) {
                    // get file extension
                    $ext = strrev(strstr(strrev($file), ".", TRUE));
                    if ($ext == 'jpg' || $ext == 'jpeg' || $ext == 'png') {
                        if ($start <= $this->data['total'] && $this->update['total'] < $finish) {
                            $this->update['images'][$i]['thumb'] = $file;
                            $this->update['images'][$i]['original'] = str_replace('thumb_', '', $file);
                            $i++;
                        }
                        $this->update['total']++;
                    }
                }
                closedir($dh);
            }
        }


        $c_paginate['total_rows'] = $this->data['total'];
        $this->pagination->initialize($c_paginate);
        $data = $this->update;
       // $data['menu'] = $this->menu->show_menu();
       // $data['all_advertising'] = $this->allproduct->all_advertising();
        $data['title'] = 'Add products';
		$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
	  	$data['adver_img'] = $this->m_products->adver_img();
		
        $this->load->view('index', $data);
    }

    private function upload_update() {
        $c_upload['upload_path'] = $this->update['dir']['original'];
        $c_upload['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|JPG|PNG|x-png';
        $c_upload['max_size'] = '500';
        $c_upload['max_width'] = '1600';
        $c_upload['max_height'] = '1200';
        $c_upload['remove_spaces'] = TRUE;

        $this->load->library('upload', $c_upload);
		
        if ($this->upload->do_upload()) {
			
	
            $img = $this->upload->data();

            // create thumbnail
            $new_image = $this->update['dir']['thumb'] . 'thumb_' . $img['file_name'];

            $c_img_lib = array(
                'image_library' => 'gd2',
                'source_image' => $img['full_path'],
                'maintain_ratio' => TRUE,
                'width' => 100,
                'height' => 100,
                'new_image' => $new_image
            );

            $this->load->library('image_lib', $c_img_lib);
            $this->image_lib->resize();
			
            $img = $this->upload->data();
            
            $this->session->set_userdata('pro_img', $img['file_name']);

          //  $this->load->model('M_addproduct');
           $data['product'] = $this->m_products->m_change_pro();
            if ($data['product'] > 0) {
				
                redirect('pages/deluppro');
            }
        } else {
            $this->data['error'] = $this->upload->display_errors();
        }
    }
	 /*********============Delete img from folder by unlink <it old variable to add products  =>>datapro >==========******/
	 function deluppro(){
				$ori_img = $this->session->userdata('getImg');
				
				unlink($this->data['dir']['original'].$ori_img);
        		unlink($this->data['dir']['thumb'].'thumb_'.$ori_img);
				
				redirect('pages/user_view_product/upsucc');
			}
	/*******End Delete img from folder **********/
	
// view products ...

	 function user_view_product() {
		$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
		 
		$config['base_url'] = site_url('pages/user_view_product');
        $config['total_rows'] = $this->db->count_all('tbl_product');
        
        $config['per_page'] = 6;
        $config["uri_segment"] = 3;
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $this->pagination->initialize($config);

        $data['pages'] = $this->pagination->create_links();

        //$data['all_advertising'] = $this->allproduct->all_advertising();
        $data['pro_list'] = $this->m_products->view_product($config['per_page'],$page);
        $data['title'] = 'Welcome to view products page';
	  	$data['adver_img'] = $this->m_products->adver_img();
      
        $this->load->view('index', $data);
    }
	// end view product of users login
	//start change photo of users statu
	function user_change_photo()
	{
		$config['upload_path'] = './assets/uploads/users/';
		$config['allowed_types'] = 'gif|jpg|jpeg|png|JPEG|JPG|PNG|x-png';
		$config['max_size']	= '100';
		$config['max_width']  = '200';
		$config['max_height']  = '200';

		$this->load->library('upload', $config);

		if ( ! $this->upload->do_upload())
		{
			$error = array('error' => $this->upload->display_errors());
                        
                        redirect("pages/user_profile/error");
			//$this->load->view('index', $error);
		}
		else
		{
			//$data = array('upload_data' => $this->upload->data());
			$upload_data = $this->upload->data();
			
			//$upload_data['file_name'];
			$this->session->set_userdata('user_img',$upload_data['file_name']);
			$ch['ch'] = $this->m_products->change_photo();
			if($ch['ch'] > 0){
				redirect("pages/deluserimg");
			}else{
				redirect("pages/user_profile/default");
			}
			
		}
	}
	function  deluserimg(){
		$getimg = $this->session->userdata('getimg');
		if($getimg != ""){
			unlink("./assets/uploads/users/".$getimg);
			redirect("pages/user_profile");
		}else{
			redirect("pages/user_profile");
		}
	}
	//end change photo .........
	
    function news() {
		
		$data['adver_img'] = $this->m_products->adver_img();
		$data['sli_pro'] = $this->m_products->sl_pro();
		
		$data['title'] = "News around the world";
		
		$config['base_url'] = site_url('backend_pages/all_news_control');
		$config['total_rows'] = $this->db->count_all('tbl_page');;
		$config['per_page'] = 7;
		$config["uri_segment"] = 3;
		$page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
		$this->pagination->initialize($config);
		
		$data['pages'] = $this->pagination->create_links();
		$data['news'] = $this->m_products->allnews($config['per_page'],$page);
		
        $this->load->view('index',$data);
    }

    function news_detail() {
		$data['adver_img'] = $this->m_products->adver_img();
		$data['sli_pro'] = $this->m_products->sl_pro();
		$data['news_detial'] = $this->m_products->news_detial();
		$data['title'] = "Detial news around the world";
        $this->load->view('index',$data);
    }

    function products() {
		$data['adver_img'] = $this->m_products->adver_img();
		$data['sli_pro'] = $this->m_products->sl_pro();
		
		$config['base_url'] = site_url('pages/products');
        $config['total_rows'] = $this->db->count_all('tbl_product');
        
        $config['per_page'] = 5;
        $config["uri_segment"] = 3;
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $this->pagination->initialize($config);

        $data['pages'] = $this->pagination->create_links();

        //$data['all_advertising'] = $this->allproduct->all_advertising();
        $data['pro_list'] = $this->m_products->allproduct($config['per_page'],$page);
        $data['title'] = 'Welcome to view products page';
      
        $this->load->view('index', $data);
    }
	function entertainment() {
		$data['adver_img'] = $this->m_products->adver_img();

		$data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
		
		$config['base_url'] = site_url('pages/entertainment');
        $config['total_rows'] = $this->db->count_all('tbl_product');
        
        $config['per_page'] = 5;
        $config["uri_segment"] = 3;
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $this->pagination->initialize($config);

        $data['pages'] = $this->pagination->create_links();

        //$data['all_advertising'] = $this->allproduct->all_advertising();
        $data['pro_list'] = $this->m_products->allproduct($config['per_page'],$page);
        $data['title'] = 'Welcome to view Enterainment page';
      
        $this->load->view('index', $data);
    }
 //function for sub menu by Thary
 
     function computers() {
		$data['adver_img'] = $this->m_products->adver_img();
		$data['sli_pro'] = $this->m_products->sl_pro();
		
		$config['base_url'] = site_url('pages/computers');
        $config['total_rows'] = $this->db->count_all('tbl_product');
        
        $config['per_page'] = 5;
        $config["uri_segment"] = 3;
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $this->pagination->initialize($config);

        $data['pages'] = $this->pagination->create_links();

        //$data['all_advertising'] = $this->allproduct->all_advertising();
        $data['computer'] = $this->m_products->com_phon($config['per_page'],$page);
        $data['title'] = 'Welcome to view products page';
      
        $this->load->view('index', $data);
    	}
	function phones() {
		$data['adver_img'] = $this->m_products->adver_img();
		$data['sli_pro'] = $this->m_products->sl_pro();
		
		$config['base_url'] = site_url('pages/phones');
        $config['total_rows'] = $this->db->count_all('tbl_product');
        
        $config['per_page'] = 5;
        $config["uri_segment"] = 3;
        $page = ($this->uri->segment(3)) ? $this->uri->segment(3) : 0;
        $this->pagination->initialize($config);

        $data['pages'] = $this->pagination->create_links();

        //$data['all_advertising'] = $this->allproduct->all_advertising();
        $data['phone'] = $this->m_products->com_phon($config['per_page'],$page);
        $data['title'] = 'Welcome to view products page';
      
        $this->load->view('index', $data);
    }
	
	
    function productdetail() {
	  	$data['adver_img'] = $this->m_products->adver_img();
		$data['sli_pro'] = $this->m_products->sl_pro();
		
        $data['pro_list'] = $this->m_products->pro_detail();
		$data['pro_com'] = $this->m_products->show_comment();
        $data['title'] = 'Welcome to detail products page';
      
        $this->load->view('index', $data);
    }

    function contact() {
	   $data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
		$data['title'] = "Contact form";
	  	$data['adver_img'] = $this->m_products->adver_img();
        $this->load->view('index', $data);
    }
   //Sending Email
   function email(){
	   $data['sli_pro'] = $this->m_products->sl_pro();// product on slide bar
	   $data['adver_img'] = $this->m_products->adver_img();
	   
	    $this->email->from($this->input->post('email'), $this->input->post('author'));
		$this->email->to('samboraeun@gmail.com');
		$this->email->cc('thary.sat@gmail.com');
		$this->email->bcc('kimsong.san@gmail.com');
		
		$this->email->subject($this->input->post('subject'));
		$this->email->message($this->input->post('text'));
		
		$this->email->send();
		
		echo $this->email->print_debugger();
		redirect("pages/contact/succ");
	   }
	 
		//Sambo commentingqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqqq
	function comment(){
		
		$this->session->set_userdata('com',$this->uri->segment(3));//com_getid
		
		$id = $this->session->userdata('com');
			
		$this->load->helper('date');
		$this->form_validation->set_rules('text','Comment','trim|required|min_length[5]');
		if($this->form_validation->run() == FALSE){
			
	   //$data['all_advertising'] = $this->products->all_advertising();
        $data['title'] = 'Welcome to Detail page';
        //$data['pro_detail'] = $this->products->detail_products();
		$data['sli_pro'] = $this->m_products->sl_pro();
		$data['adver_img'] = $this->m_products->adver_img();
        //$data['menu'] = $this->menu->show_menu();
        $this->load->view('index', $data);	
		
		}else{
			
			//$this->products->commenting();
			
			//$data['com']= $this->products->select();
			//$data['all_advertising'] = $this->products->all_advertising();
			$data['title'] = 'Welcome to Detail page';
			//$data['pro_detail'] = $this->products->detail_products();
			$data['sli_pro'] = $this->m_products->sl_pro();
			$data['adver_img'] = $this->m_products->adver_img();
			$data['com'] = $this->m_products->comment();
			//$this->load->view('index', $data);
			redirect('pages/productdetail'.'/'.$this->session->userdata('com'));
		}
	}
	//end of commenting
}

/* End of file welcome.php */
/* Location: ./system/application/controllers/welcome.php */